package com.ls.lserp.modules.job.service;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ls.lserp.common.service.CrudService;
import com.ls.lserp.common.utils.DateUtils;
import com.ls.lserp.common.utils.ResourcesUtil;
import com.ls.lserp.modules.produce.dao.ProducePlanDao;
import com.ls.lserp.modules.produce.entity.ProducePlan;
import com.ls.lserp.modules.sys.dao.SequenceDao;
import com.ls.lserp.modules.sys.entity.Sequence;
import com.ls.lserp.modules.sys.utils.SequenceUtil;

/**
 * @ClassName : JobNoResetService
 * @Description : 重新设置企业定制生产号
 *
 * @author : zhigang.gao
 * @date : 2017年6月27日
 */
@Service
@Transactional(readOnly = false)
public class JobNoResetService extends CrudService<SequenceDao, Sequence> {

	@Autowired
	private SequenceDao sequenceDao;
	//生产计划
    @Autowired
    private ProducePlanDao producePlanDao;

    
	/**
	 * @Title : jobNoResetServer
	 * @Description : 每周三重新设置企业的订单生产号
	 * @throws SQLException
	 * @return : void
	 * 
	 * @author : zhigang.gao
	 * @throws ParseException 
	 * @throws SQLException
	 * @date : 2017年6月27日
	 */
	public void jobNoResetServer() throws SQLException, ParseException {

		// 系统日期时间
		String nowDateTime = new String();

		// 获取开始日期时间
		nowDateTime = DateUtils.getDateTime();
		// logger.info("--------------------" + nowDateTime + "重新设置企业定制生产号batch [" +
		// this.getClass().getName()
		// + "] Start ------------------------");
		reset4MJobNoServer();

		reset4HJobNoServer();


		// 获取开始日期时间
		nowDateTime = DateUtils.getDateTime();
		// logger.info("--------------------" + nowDateTime + "重新设置企业定制生产号batch [" +
		// this.getClass().getName()
		// + "] OVER------------------------");
	}
	
	public void resetProductionNo() throws SQLException, ParseException {
		sequenceDao.updateKeyWord("orderProductionNo", "", 0);
	}

	/**
	 * @Title : reset4HJobNoServer
	 * @Description : 每周三重新设置机缝生产号
	 * @throws SQLException
	 * @return : void
	 * 
	 * @author : zhigang.gao
	 * @throws ParseException 
	 * @throws SQLException 
	 * @date : 2017年6月27日
	 */
	public void reset4HJobNoServer() throws SQLException, ParseException {

		// 序号实体类
		Sequence sequence = new Sequence();

		// 4H 手缝当前编码
		String str4HKeyWord = new String();
		long int4HNumber;
		//旧日期
		Date oldInt4HNumber = SequenceUtil.getShipTime("4M");
		//旧交货日期加
		Calendar date = Calendar.getInstance();
		date.setTime(oldInt4HNumber);
		date.set(Calendar.DATE, date.get(Calendar.DATE) + 1);
		oldInt4HNumber = date.getTime();
		String[] arrKeyList = ResourcesUtil.getConfig("4H_KEY_CODE").split(",");

		// 机缝生产号设置
		sequence = sequenceDao.getKeyWord(ResourcesUtil.getConfig("4H_CODE"));
		int4HNumber = DateUtils.getDateToPull(sequence.getNumber());

		Calendar c = Calendar.getInstance();
		c.set(Calendar.YEAR, Integer.parseInt(String.valueOf(int4HNumber).substring(0, 4)));
		c.set(Calendar.MONTH, Integer.parseInt(String.valueOf(int4HNumber).substring(4, 6)) - 1);
		c.set(Calendar.DATE, Integer.parseInt(String.valueOf(int4HNumber).substring(6)));
		c.setFirstDayOfWeek(Calendar.FRIDAY);
		c.setMinimalDaysInFirstWeek(7);
		// 第几周
		int week = c.get(Calendar.WEEK_OF_MONTH);
		String keyNumber = "";
		String keyWord = "";
		if ((c.get(Calendar.MONTH) + 1) < 10) {
			keyNumber = "0" + String.valueOf(c.get(Calendar.MONTH) + 1);
		} else {
			keyNumber = String.valueOf(c.get(Calendar.MONTH) + 1);
		}

		keyWord = arrKeyList[week - 1];

		str4HKeyWord = keyNumber + keyWord;

		sequenceDao.updateKeyWord(ResourcesUtil.getConfig("4H_CODE"), str4HKeyWord, int4HNumber);
		//创建新的生产计划
		createNewPlan("P.MU.M." + str4HKeyWord,oldInt4HNumber,SequenceUtil.getShipTime("4M"));

		logger.info("--------机缝------------" + str4HKeyWord + "---" + int4HNumber);

	}

	/**
	 * @Title : reset4MJobNoServer
	 * @Description : 每周三重新设置手缝生产号
	 * @throws SQLException
	 * @return : void
	 * 
	 * @author : zhigang.gao
	 * @throws SQLException
	 * @throws ParseException
	 * @date : 2017年6月27日
	 */
	public void reset4MJobNoServer() throws SQLException,ParseException {

		// 序号实体类
		Sequence sequence = new Sequence();

		// 4M 手缝当前编码
		String str4MKeyWord = new String();
		long int4MNumber;
		//旧日期
		Date oldInt4MNumber = SequenceUtil.getShipTime("4M");
		//旧交货日期加
		Calendar date = Calendar.getInstance();
		date.setTime(oldInt4MNumber);
		date.set(Calendar.DATE, date.get(Calendar.DATE) + 1);
		oldInt4MNumber = date.getTime();
		String keyNumber = "";
		String keyWord = "";

		// 手缝生产号设置
		sequence = sequenceDao.getKeyWord(ResourcesUtil.getConfig("4M_CODE"));
		str4MKeyWord = sequence.getKeyWord();
		int4MNumber = DateUtils.getDateToPull(sequence.getNumber());

		Calendar c = Calendar.getInstance();
		c.set(Calendar.YEAR, Integer.parseInt(String.valueOf(int4MNumber).substring(0, 4)));
		c.set(Calendar.MONTH, Integer.parseInt(String.valueOf(int4MNumber).substring(4, 6)) - 1);
		c.set(Calendar.DATE, Integer.parseInt(String.valueOf(int4MNumber).substring(6)));
		c.setFirstDayOfWeek(Calendar.FRIDAY);
		c.setMinimalDaysInFirstWeek(7);
		// 第几周
		int week = c.get(Calendar.WEEK_OF_MONTH);

		if ((c.get(Calendar.MONTH) + 1) < 10) {
			keyNumber = "0" + String.valueOf(c.get(Calendar.MONTH) + 1);
		} else {
			keyNumber = String.valueOf(c.get(Calendar.MONTH) + 1);
		}

		keyWord = "M";

		str4MKeyWord = keyNumber + keyWord + week;

		sequenceDao.updateKeyWord(ResourcesUtil.getConfig("4M_CODE"), str4MKeyWord, int4MNumber);
		//创建新的生产计划
		createNewPlan("P.MU.H." + str4MKeyWord,oldInt4MNumber,SequenceUtil.getShipTime("4H"));

		logger.info("--------手缝------------" + str4MKeyWord + "---" + int4MNumber);

	}
	/**
	 * 创建生产计划
	 * @throws ParseException 
	 */
	public void createNewPlan(String planNo,Date beginDate,Date endDate) throws ParseException{
		//日期格式化
		//SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		ProducePlan producePlan = new ProducePlan();
		//创建一条生产计划
		producePlan.setPlanNO(planNo);
		producePlan.setOgnizationID(78);
		producePlan.preInsert();
		producePlan.setUserGroupID(27L);
		producePlan.setPlanBeginDate(beginDate);
		producePlan.setPlanEndDate(endDate);
		producePlan.setPlanNumber(20);
		producePlan.setPlanEfficiency(new BigDecimal(3));
		producePlan.setPlanPeriod(new BigDecimal((endDate.getTime()-beginDate.getTime())/(24*60*60*1000)+1));
		producePlan.setPlanStatus(1);
		producePlan.setPlanQuantity(new BigDecimal(0));
		producePlanDao.insert(producePlan);
	}
}
